<?php

// 9/12/2009: immanuel192 write this action

// this action shows all album in selected order

if (!defined("EXPONENT")) exit("");
  global $user;
  global $db;
// get order id
  $id=-1;
if (isset($_GET['id'])) $id=intval($_GET['id']);
// check order id
 $temp=$db->selectObject("orders","id = {$id}");
 @$note=$temp->note;
 if (!$temp || ((!$user->is_admin || !$user->is_acting_admin) && ($temp->user_id != $user->id))) $id=-1;
if (exponent_sessions_loggedIn() && ($id>0)){
  $template = new template('storemodule','_view_order',$loc);
  
  $cache=$db->selectObjectsBySql("
       SELECT {$db->prefix}album.*,
              {$db->prefix}album_format.name as format_name,
              {$db->prefix}order_items.* FROM {$db->prefix}order_items INNER JOIN {$db->prefix}album ON {$db->prefix}order_items.item_id = {$db->prefix}album.id INNER JOIN {$db->prefix}album_format ON {$db->prefix}album_format.id={$db->prefix}album.format WHERE order_id={$id}");
    // generate icon
    foreach ($cache as $key=>$value){
      // set icon
      if (strpos(strtolower($value->format_name),"dvd") >= 0)
        $value->icon="dvd";
      else if (strpos(strtolower($value->format_name),"cd") >= 0)
         $value->icon="cd";
      else
         $value->icon="music";
      
      if ($value->image_id == 0) {
		    $value->picpath = '';
     } else {
		      $file = $db->selectObject('file', 'id='.$value->image_id);
    		$value->picpath = $file->directory.'/'.$file->filename;
	     }
			
	     // for rating
    	$rate_1 = floor($value->rate);
	    $rate_half = $value->rate - $rate_1;
      $rate_2 = 5- $rate_1 - (($rate_half > 0)?1:0);
      $rate=array();
      for ($j=1;$j<=$rate_1;$j++)
        $rate[$j]=1; // for active rate
      if ($rate_half > 0)
        $rate[$rate_1+1]=3; // for rate half
      for ($j=5-$rate_2+1;$j<=5;$j++)
        $rate[$j]=2; // for active rate
      $value->rate=$rate;
	   // end of rating
			
	   // for format_type
	   $temp=$db->selectObject("album_format","id = ".$value->format);
	   $value->format_text = $temp->name;
	   // end of format type
	   // for genre 
      $temp=$db->selectObject("album_genre","id = ".$value->genre);
      $value->genre_text = $temp->name;
	   // end of genre   
     // count how many musics
     $musics=$db->countObjects('musics','album_id='.$value->id );
     $value->musics = $musics;
     
     // for location source
     $temp=unserialize($value->location_data);
     $value->src=$temp->src;
    }
    
    // get ship fee
    $is_logged=exponent_sessions_loggedIn();
    if ($is_logged){
     // get user location
     $country=strtolower($user->user_address->country);
     if ($country == "usa" || $country == "canada")
      $template->assign("ship_fee",0);
     else
      $template->assign("ship_fee",5);
    } else {
      $template->assign("ship_fee",5);
    }
    
    $template->assign("orders",$cache);
    $template->assign("note",$note);
    // get language from album module and storemodule Default Views
    $more_lang=exponent_lang_loadFile("modules/albummodule/views/Default.php");
    $more_lang2=exponent_lang_loadFile("modules/storemodule/views/Default.php");
    $more_lang3=exponent_lang_loadFile("modules/storemodule/step.php");
    $more_lang=array_merge($more_lang, $more_lang2, $more_lang3);
    $template->assign("more_lang",$more_lang);
    $template->assign("is_logged",exponent_sessions_loggedIn());
	$template->output();
} // for if exponent_sessions_loggedIn
else
{
 // redirect to homepage
	echo "<script language=javascript>document.location='".URL_BASE."/index.php';</script>";
	die();
}
?>
